from pandac.PandaModules import *                       # Basic Panda modules
from direct.showbase.DirectObject import DirectObject   # For event handling
from direct.actor.Actor import Actor                    # For animated models
from direct.interval.IntervalGlobal import *            # For compound intervals
from direct.task import Task                            # For update functions
from direct.particles.ParticleEffect import ParticleEffect  #For particles

import random
from direct.particles.ParticleEffect import ParticleEffect

class Fire(object):
    def __init__(self, x = 0, y = 0, z = 0, startsize = .5, maxsize = 5.0, growspeed = .5, starttime = 0.0, xweight = 0, yweight = 0):
        """Initializes a fire object"""
        #Position
        self.xPos = x
        self.yPos = y
        self.zPos = z
        
        #Initial Starting Data
        self.startSize = startsize
        self.maxSize = maxsize
        self.growSpeed = growspeed
        self.startTime = starttime
        self.maxSpreads = 3
        
        #Current Information
        self.size = startsize
        self.timesSpread = 0
        
        #Timings
        self.lastUpdate = starttime
        self.lastSpreadCheck = 0
        
        #Direction the fire is spreading
        self.xWeight = xweight
        self.yWeight = yweight
        
        #Load the model and attach to render
        self.effect = ParticleEffect()
        self.effect.loadConfig("Art/models/Fire.ptf")   
        self.effect.setPos(x,y,z)
        self.effect.start(parent = render, renderParent = render)
        
        #Whether fire needs to spread
        self.spawnNeeded = False
        self.health = 100
        self.done = False
        
        self.setupCollision()
        
        
    def update(self, firelist, task):
        """Updates the fire size and checks if it should spread"""
        curTime = task.time
        elapsedTime = (curTime - self.lastUpdate)
        self.lastUpdate = curTime
        
        #Cause the fire to grow, check size boundary
        self.size += self.growSpeed * elapsedTime
        
        if(self.size > self.maxSize):
            self.size = self.maxSize
            
            
        #Check for spreading chance if fire big enough and enough time (15s) has elapsed
        if(len(firelist) <= firelist[0]):
            if self.timesSpread < self.maxSpreads:
                if self.size >= self.maxSize * (2.0/3):
                    if curTime - self.lastSpreadCheck > 3:
                        self.lastSpreadCheck = curTime
                        roll = random.uniform(1,100)
                        if roll > 90:
                            self.spawnNeeded = True
                
        if self.done:
            pass
        
    def cleanup(self):
        self.effect.cleanup()
        
    def setupCollision(self):
        self.fireGroundSphere = CollisionSphere(0,0,0,2)
        self.fireGroundCol = CollisionNode('fireSphere') 
        self.fireGroundCol.addSolid(self.fireGroundSphere) 
        self.fireGroundColNp = self.effect.attachNewNode(self.fireGroundCol)
        #self.fireGroundColNp.show()
        self.fireGroundCol.setIntoCollideMask(BitMask32(0x1))
        
        #print self.fireGroundColNp
        base.fire_dict[self.fireGroundColNp] = self
        
    def isDone(self):
        if(self.health <= 0):
            self.done = True
            self.effect.disable()
            return True